import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../main.dart';

/// Create by kyluzoi
/// Date: 2021-08-16
///
/// Description: 请在使用时初始化 GlobalKey AppGlobalKey = GlobalKey(); 用于我们的 context
/// 这里设计成单例，同一时间只允许一次提示框被创建，所以注意调用逻辑，后续优化成队列

class ToastUtils {
  static final ToastUtils _instance = ToastUtils._internal();

  factory ToastUtils() => _instance;

  ToastUtils._internal();

  OverlayEntry? _overlayEntry;
  bool _showing = false;
  Timer? _timer;

  // txt
  void showText(String text, {BuildContext? context}) {
    Widget yiCardText = Card(
      color: Colors.black87,
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Text(
          text,
          style: TextStyle(fontSize: 15, color: Colors.white),
        ),
      ),
    );
    _autoRemove(yiCardText, text, context: context);
  }

  // success
  void showSuccessText(String text, {BuildContext? context}) {
    _showIconInfoText(text, "images/toastlib/alter_ok.png", context);
  }

  // info
  void showInfoText(String text, {BuildContext? context}) {
    _showIconInfoText(text, "images/toastlib/alter_info.png", context);
  }

  // error
  void showErrorText(String text, {BuildContext? context}) {
    _showIconInfoText(text, "images/toastlib/alter_wro.png", context);
  }

  // hud + text
  void showHudText(String text, {BuildContext? context}) {
    Widget hudText = Container(
      height: 80,
      decoration: BoxDecoration(
        color: Colors.black54,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      padding: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          Container(
            width: 60,
            height: 40,
            padding: EdgeInsets.all(10),
            child: Theme(
              data: ThemeData(
                cupertinoOverrideTheme: CupertinoThemeData(
                  brightness: Brightness.dark,
                ),
              ),
              child: CupertinoActivityIndicator(
                radius: 16,
              ),
            ),
          ),
          Material(
            color: Colors.transparent,
            child: Text(
              text,
              style: TextStyle(
                fontSize: 14,
                color: Colors.white,
              ),
            ),
          ),
        ],
      ),
    );
    _handMovementRemove(hudText, context: context);
  }

  // hud
  void showHud({BuildContext? context}) {
    Widget hud = Container(
      width: 80,
      height: 80,
      decoration: BoxDecoration(
        color: Colors.black54,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      child: Theme(
        data: ThemeData(
          cupertinoOverrideTheme: CupertinoThemeData(
            brightness: Brightness.dark,
          ),
        ),
        child: CupertinoActivityIndicator(
          radius: 16,
        ),
      ),
    );
    _handMovementRemove(hud, context: context);
  }

  // dismiss
  void hideHud() {
    if (_overlayEntry != null) {
      _overlayEntry!.remove();
      _overlayEntry = null;
      _timer?.cancel();
    }
  }

  void dismissHud() {
    _showing = false;
    _overlayEntry!.markNeedsBuild();
    Future.delayed(Duration(milliseconds: 100), () {
      if (_overlayEntry != null) {
        _overlayEntry!.remove();
        _overlayEntry = null;
        _timer?.cancel();
      }
    });
  }

  void _showIconInfoText(String text, String imageName, BuildContext? context) {
    Widget iconText = Container(
      height: 80,
      decoration: BoxDecoration(
        color: Colors.black87,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      padding: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          Container(
            width: 60,
            height: 40,
            padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
            child: Image.asset(
              imageName,
              width: 40,
              height: 40,
            ),
          ),
          Material(
            color: Colors.transparent,
            child: Text(
              text,
              style: TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        ],
      ),
    );
    _autoRemove(iconText, text, context: context);
  }

  void _autoRemove(Widget child, String text, {BuildContext? context}) {
    hideHud();
    // 简单写个消失的动画，可自行自定义
    _showing = true;
    _setOverlayEntry(child, context);
    _removeText(text);
  }

  void _handMovementRemove(Widget child, {BuildContext? context}) {
    hideHud();
    _showing = true;
    _setOverlayEntry(child, context);
  }

  // 预留了拓展动画的fuc
  void _setOverlayEntry(Widget child, BuildContext? context,
      {Function? animationFc}) {
    if (context == null) {
      context = AppGlobalKey.currentState?.context;
    }
    if (context == null) throw ("Error: 请检查是否有传入 Content 或 配置全局 Context.");
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
          builder: (BuildContext context) => WillPopScope(
                onWillPop: () async {
                  print("back");
                  return false;
                },
                child: Positioned(
                  top: MediaQuery.of(context).padding.top + kToolbarHeight,
                  child: Container(
                    alignment: Alignment.center,
                    color: Colors.transparent,
                    width: MediaQuery.of(context).size.width,
                    height: MediaQuery.of(context).size.height -
                        kToolbarHeight -
                        MediaQuery.of(context).padding.top,
                    padding: EdgeInsets.only(
                        bottom: kToolbarHeight +
                            MediaQuery.of(context).padding.top),
                    child: _createAnimation(child),
                  ),
                ),
              ));
      //插入到整个布局的最上层
      Overlay.of(context)!.insert(_overlayEntry!);
    } else {
      //重新绘制UI，类似setState
      _overlayEntry!.markNeedsBuild();
    }
  }

  // 传入text，根据字数判断显示的时长(1-3s)
  void _removeText(String text) {
    if (_overlayEntry != null) {
      // 显示时间
      int _showTime = text.length * 200;
      if (_showTime < 1000) {
        _showTime = 1000;
      } else if (_showTime > 3000) {
        _showTime = 3000;
      }
      _timer = Timer(Duration(milliseconds: _showTime), () {
        _showing = false;
        _overlayEntry!.markNeedsBuild();
        Future.delayed(Duration(milliseconds: 200), () {
          _overlayEntry!.remove();
          _overlayEntry = null;
          _timer?.cancel();
        });
      });
    }
  }

  _createAnimation(Widget child) {
    return AnimatedOpacity(
      opacity: _showing ? 1.0 : 0.0,
      duration:
          _showing ? Duration(milliseconds: 300) : Duration(milliseconds: 200),
      child: child,
    );
  }
}
